(function () {
  var vm = window.vm = new Vue({
    el: '.aui-wrapper',
    i18n: window.SITE_CONFIG.i18n,
    mixins: [window.SITE_CONFIG.mixinViewModule],
    data: function () {
      return {
        mixinViewModuleOptions: {
          getDataListURL: '/pay/notice/page',
          getDataListIsPage: true,
          exportURL: '/pay/notice/export',
          deleteURL: '/pay/notice',
          deleteIsBatch: true
        },
        pickerOptions: {
            shortcuts: [
              {
                text: "今天",
                onClick(picker) {
                  picker.$emit("pick", new Date());
                }
              },
              {
                text: "昨天",
                onClick(picker) {
                  const date = new Date();
                  date.setTime(date.getTime() - 3600 * 1000 * 24);
                  picker.$emit("pick", date);
                }
              },
              {
                text: "一周前",
                onClick(picker) {
                  const date = new Date();
                  date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
                  picker.$emit("pick", date);
                }
              }
            ]
          },
          dataListLoading: false,
          deptList:[],
          deptListVisible : false,
          dataForm: {
            id: "",
            merchantname: '',
            starttime: null,
            endtime: null
          }
    
      }
    },
    components: {
      'add-or-update': fnAddOrUpdateComponent()
    },
    methods: {
        getDeptList: function () {
            return vm.$http.get('/sys/dept/list').then(function (res) {
              if (res.data.code !== 0) {
                return vm.$message.error(res.data.msg);
              }
              vm.deptList = res.data.data;
            }).catch(function () {});
          },
        deptListTreeCurrentChangeHandle: function (data, node) {
            vm.dataForm.merchantid = data.id;
            vm.dataForm.merchantname = data.name;
            vm.deptListVisible = false;
          } ,
          clear :function (data, node) {
            vm.dataForm.merchantid = '';
          }
     },
    beforeCreate: function () {
      vm = this;
    },
    created: function () {
		vm.getDeptList();
    }
  });
})();

/**
 * add-or-update组件
 */
function fnAddOrUpdateComponent () {
  var self = null;
  return {
    template: '\
      <el-dialog :visible.sync="visible" :title="!dataForm.id ? $t(\'add\') : $t(\'update\')" :close-on-click-modal="false" :close-on-press-escape="false" :fullscreen="true">\
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()" label-width="120px">\
    	  <el-form-item v-if="$hasPermission(\'\sys:pay:showmerchantid\'\)" label="公告类型" prop="configtype">\
  		<el-select v-model="dataForm.noticeType" placeholder="公告类型" @change="getOption" size="medium" clearable>\
				<el-option v-for="item in configtypeOptions" :key="item.value" :label="item.label" :value="item.value"></el-option>\
  		</el-select>\
        </el-form-item>\
  	<el-form-item  v-if="$hasPermission(\'\sys:pay:showmerchantid\'\) && isShowId" prop="merchantname" :label="$t(\'user.deptName\')" class="dept-list">\
      <el-popover v-model="deptListVisible" ref="deptListPopover" placement="bottom-start" trigger="click">\
    	<div class=\'tree\' style=\'overflow-y:scroll;overflow-x: auto;height: 200px;width:200px;\'>\
        <el-tree\
          :data="deptList"\
          :props="{ label: \'name\', children: \'children\' }"\
          node-key="id"\
          ref="deptListTree"\
          :highlight-current="true"\
          :expand-on-click-node="false"\
          accordion\
          @current-change="deptListTreeCurrentChangeHandle">\
        </el-tree>\
    	</div>\
      </el-popover>\
      <el-input v-model="dataForm.merchantname" v-popover:deptListPopover :readonly="true" :placeholder="$t(\'user.deptName\')"></el-input>\
    </el-form-item>\
          <el-form-item label="公告标题" prop="noticeTitle">\
            <el-input v-model="dataForm.noticeTitle" placeholder="公告标题"></el-input>\
          </el-form-item>\
          <el-form-item label="公告内容" prop="noticeMessage">\
            <el-input v-model="dataForm.noticeMessage" placeholder="公告内容"></el-input>\
          </el-form-item>\
        </el-form>\
        <template slot="footer">\
          <el-button @click="visible = false">{{ $t(\'cancel\') }}</el-button>\
          <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t(\'confirm\') }}</el-button>\
        </template>\
      </el-dialog>\
    ',
    data: function () {
      return {
        visible: false,
        isShowId:true,
        deptList: [],
        deptListVisible: false,
        dataForm: {
          id: '',
          merchantid: '',
          merchantname: '',
          noticeTitle: '',
          noticeType: '',
          noticeMessage: '',
          creator: '',
          createtime: '',
          updater: '',
          updatetime: ''
        },
        configtypeOptions: [
            {label:'系统',value:'0'},
            {label:'商户',value:'1'},
          ]
      }
    },
    computed: {
      dataRule: function () {
        return {
          merchantid: [
            { required: true, message: this.$t('validate.required'), trigger: 'blur' }
          ],
          merchantname: [
            { required: true, message: this.$t('validate.required'), trigger: 'blur' }
          ],
          noticeTitle: [
            { required: true, message: this.$t('validate.required'), trigger: 'blur' }
          ],
          noticeType: [
            { required: true, message: this.$t('validate.required'), trigger: 'blur' }
          ],
          noticeMessage: [
            { required: true, message: this.$t('validate.required'), trigger: 'blur' }
          ],
          createtime: [
            { required: true, message: this.$t('validate.required'), trigger: 'blur' }
          ],
          updater: [
            { required: true, message: this.$t('validate.required'), trigger: 'blur' }
          ],
          updatetime: [
            { required: true, message: this.$t('validate.required'), trigger: 'blur' }
          ]
        }
      }
    },
    beforeCreate: function () {
      self = this;
    },
    methods: {
      init: function () {
        self.visible = true;
        self.getDeptList(),
        self.$nextTick(function () {
          self.$refs['dataForm'].resetFields();
          if (self.dataForm.id) {
            self.getInfo();
          }
        });
      },

      getOption:function(value){
        if(value == 0){
          self.isShowId = false;
        }else{
          self.isShowId = true
        }
      },
      // 获取信息
      getInfo: function () {
        self.$http.get('/pay/notice/' + self.dataForm.id).then(function (res) {
          if (res.data.code !== 0) {
            return self.$message.error(res.data.msg);
          }
          self.dataForm = _.merge({}, self.dataForm, res.data.data);
        }).catch(function () {});
      },
      getDeptList: function () {
          return self.$http.get('/sys/dept/list').then(function (res) {
            if (res.data.code !== 0) {
              return self.$message.error(res.data.msg);
            }
            self.deptList = res.data.data;
          }).catch(function () {});
        },
        // 所属部门树, 选中
        deptListTreeCurrentChangeHandle: function (data, node) {
          self.dataForm.merchantid = data.id;
          self.dataForm.merchantname = data.name;
          self.deptListVisible = false;
        },
      // 表单提交
      dataFormSubmitHandle: _.debounce(function () {
        self.$refs['dataForm'].validate(function (valid) {
          if (!valid) {
            return false;
          }
          self.$http[!self.dataForm.id ? 'post' : 'put']('/pay/notice', self.dataForm).then(function (res) {
            if (res.data.code !== 0) {
              return self.$message.error(res.data.msg);
            }
            self.$message({
              message: self.$t('prompt.success'),
              type: 'success',
              duration: 500,
              onClose: function () {
                self.visible = false;
                self.$emit('refresh-data-list');
              }
            });
          }).catch(function () {});
        });
      }, 1000, { 'leading': true, 'trailing': false })
    }
  }
};